 
#!/usr/bin/env python

# --------------------------------------
# projects/python/collatz/TestCollatz.py
# Copyright (C) 2009
# Glenn P. Downing
# --------------------------------------

# To run the tests
#     TestCollatz.py

# To document the tests
#     pydoc -w TestCollatz

import main
import unittest

# ----------
# TestReader
# ----------

class TestReader (object) :
    def __init__ (self, s) :
        self.s = s

    def read (self) :
        return self.s

# ----------
# TestWriter
# ----------

class TestWriter (object) :
    def str (self) :
        return self.s

    def write (self, *a) :
        self.s = str(a[0])
        for w in a[1:] :
            self.s += ' '
            self.s += str(w)
        self.s += '\n'

# -----------
# TestCollatz
# -----------

class TestCollatz (unittest.TestCase) :
    # ----
    # read
    # ----

    def test_read (self) :
        reader = TestReader('1 10\n')
        main.my_read(reader)
        self.assert_(main.i ==  1)
        self.assert_(main.j == 10)

    # ----
    # eval
    # ----

    def test_eval1 (self) :
        main.i =  1
        main.j = 10
        main.v =  0
        main.my_eval()
        self.assert_(main.v == 20)

    def test_eval2 (self) :
        main.i = 100
        main.j = 200
        main.v =   0
        main.my_eval()
        self.assert_(main.v == 125)

    def test_eval3 (self) :
        main.i = 201
        main.j = 210
        main.v =   0
        main.my_eval()
        self.assert_(main.v == 89)

    def test_eval4 (self) :
        main.i =  900
        main.j = 1000
        main.v =    0
        main.my_eval()
        self.assert_(main.v == 174)
    
    def test_eval5 (self) :
        main.i = 9000
        main.j = 10000
        main.v =    0
        main.my_eval()
        self.assert_(main.v == 260)
    
    def test_eval6 (self) :
        main.i =  34534
        main.j =  45675
        main.v =  0
        main.my_eval()
        self.assert_(main.v == 324)

    def test_eval7 (self) :
        main.i = 893350
        main.j = 999999
        main.v =   0
        main.my_eval()
        self.assert_(main.v == 507)

    def test_eval8 (self) :
        main.i = 200
        main.j = 200
        main.v =   0
        main.my_eval()
        self.assert_(main.v == 27)

    def test_eval9 (self) :
        main.i = 5000
        main.j = 7500
        main.v =    0
        main.my_eval()
        self.assert_(main.v == 262)
    
    def test_eval10 (self) :
        main.i = 45634 
        main.j = 60345
        main.v =    0
        main.my_eval()
        self.assert_(main.v == 340)

    def test_eval11 (self) :
        main.i = 10
        main.j = 1
        main.v =  0
        main.my_eval()
        self.assert_(main.v == 20)

    def test_eval12 (self) :
        main.i = 200
        main.j = 100
        main.v =   0
        main.my_eval()
        self.assert_(main.v == 125)

    def test_eval13 (self) :
        main.i = 210
        main.j = 201
        main.v =   0
        main.my_eval()
        self.assert_(main.v == 89)

    def test_eval14 (self) :
        main.i = 1000
        main.j = 900
        main.v =    0
        main.my_eval()
        self.assert_(main.v == 174)
    
    def test_eval15 (self) :
        main.i = 5
        main.j = 6
        main.v =    0
        main.my_eval()
        self.assert_(main.v == 9)
    
    def test_eval16 (self) :
        main.i =  100
        main.j =  100
        main.v =  0
        main.my_eval()
        self.assert_(main.v == 26)

    def test_eval17 (self) :
        main.i = 597
        main.j = 201
        main.v =   0
        main.my_eval()
        self.assert_(main.v == 144)

    def test_eval18 (self) :
        main.i = 1234
        main.j = 5678
        main.v =   0
        main.my_eval()
        self.assert_(main.v == 238)

    def test_eval19 (self) :
        main.i = 10726
        main.j = 20349
        main.v =    0
        main.my_eval()
        self.assert_(main.v == 279)
    
    def test_eval20 (self) :
        main.i = 1235
        main.j = 235236
        main.v =    0
        main.my_eval()
        self.assert_(main.v == 443)

    def test_eval21 (self) :
        main.i = 1231
        main.j = 234234
        main.v =   0
        main.my_eval()
        self.assert_(main.v == 443)

    def test_eval22 (self) :
        main.i = 287158
        main.j = 459254
        main.v =    0
        main.my_eval()
        self.assert_(main.v == 449)
    
    def test_eval23 (self) :
        main.i = 253
        main.j = 745
        main.v =    0
        main.my_eval()
        self.assert_(main.v == 171)
	
    # ----------
    # eval_range
    # ----------

    def test_eval_range1 (self) :
        main.v = main.eval_range(1, 10, 0)
        self.assert_(main.v == 20)

    def test_eval_range2 (self) :
        main.v = main.eval_range(100, 200, 0)
        self.assert_(main.v == 125)

    def test_eval_range3 (self) :
        main.v = main.eval_range(201, 210, 0)
        self.assert_(main.v == 89)

    def test_eval_range4 (self) :
        main.v = main.eval_range(900, 1000, 0)
        self.assert_(main.v == 174)
    
    def test_eval_range5 (self) :
        main.v = main.eval_range(900, 1000, 0)
        self.assert_(main.v == 174)
	
    #---------
    #cycle_len
    #---------
    
    def test_cycle_len6 (self) :
        c = main.cycle_len(8)
        self.assert_(c == 4)

    def test_cycle_len7 (self) :
        c = main.cycle_len(100)
        self.assert_(c == 26)

    def test_cycle_len8 (self) :
        c = main.cycle_len(500)
        self.assert_(c == 111)

    def test_cycle_len9 (self) :
        c = main.cycle_len(999)
        self.assert_(c == 50)
    
    def test_cycle_len10 (self) :
        c = main.cycle_len(113424)
        self.assert_(c == 62)
	

    # -----
    # print
    # -----

    def test_print (self) :
        main.i =  1
        main.j = 10
        main.v = 20
        writer = TestWriter()
        main.my_print(writer)
        self.assert_(writer.str() == '1 10 20\n')

if __name__ == "__main__" :
    unittest.main()